define(["../com"], function() {
	
	var carNo = UrlParam.getParam("carNo");
	var $carNos = $(".modal .list-group");
	
	$carNos.parseHtml().empty();
	
	for (var i = 1; i <=12; i++) {
		var j = (i < 10 ? "0":"")  + i;
		$("#insureDate_m").append("<option value="+ j +">"+ j +"月</option>");
	}
	for (var i = 1; i <=31; i++) {
		var j = (i < 10 ? "0":"")  + i;
		$("#insureDate_d").append("<option value="+ j +">"+ j +"日</option>");
	}
	
	$(document).on('click', ".modal .list-group a", function() {
		$carNos.find("a").removeClass("active");
		$(this).addClass("active");
		$(".modal").modal("hide");
		setCarNo($(this).text());
	})
	
	$("#bt-submit").click(function() {
		var params = getParams();
		if (!params) {
			return;
		}
		
		$.post(g_domain + "/user/setcare", params, function(data) {
			if (data.status !== 0) {
				data.code == "0" ? void(0) : alert(data.message);
				return;
			}
			alert("设置成功！");
			wx.closeWindow();
		});
	});
	
	loadCarList();
	
	function loadCarList() {
		$.get(g_domain + "/car?r=" + Math.random(), function(data) {
			if (data.status !== 0) {
				data.code == "0" ? void(0) : alert(data.message);
				return;
			}
			$carNos.empty().parseHtml({ 
				data: data.result.list,
				beforeNode: {
					root: function(car) {
						car.activeClass = "";
						if (!carNo && car.isDefault) {
							setCarNo(car.carNo);
							car.activeClass = "active";
						} else if (carNo && carNo == car.carNo) {
							setCarNo(carNo);
							car.activeClass = "active";
						}
					} 
				}
			});
		});
	}
	
	function getParams() {
		var careMiles = $("#careMiles").val();
		var careMons = $("#careMons").val();
		var cardDate = $("#cardDate").val();
		var insureDate = $("#insureDate_m").val() + "-" + $("#insureDate_d").val();
		if (!carNo) {
			alert("未指定车牌号");
			return;
		}
		if (!careMiles) {
			alert("保养间隔里程不能为空");
			return;
		} else if (!new RegExp(/^\d+$/).test(careMiles)) {
			alert("保养间隔里程请输入数字");
			return;
		}
		if (careMons && !new RegExp(/^\d+$/).test(careMons)) {
			alert("保养间隔日期请输入数字");
			return;
		}
		if (cardDate && !new RegExp(/^\d{4}-\d{1,2}-\d{1,2}$/).test(cardDate)) {
			alert("行驶证登记日期请输入正确的日期，例：yyyy年mm月dd日");
			return;
		}
		if (insureDate && !new RegExp(/^\d{1,2}-\d{1,2}$/).test(insureDate)) {
			alert("保险购买日期请输入正确的日期，例：mm月dd日");
			return;
		}
		
		return {
			carCardsNo: carNo,
			careMiles: careMiles,
			careMons: careMons,
			cardDate: cardDate,
			insureDate: insureDate
		};
	}
	
	function setCarNo(value) {
		$("#curCarNo-text").html(value);
		carNo = value;
		loadData(carNo);
	}
	
	function loadData(carNo) {
		$.post(g_domain + "/user/qrycare", {carNo: carNo}, function(data) {
			if (data.status !== 0) {
				data.code == "0" ? void(0) : alert(data.message);
				return;
			}

			var apiData = $.parseJSON(data.result).response;
			if (apiData.length == 0) {
				return;
			}
			careData = apiData[0];
			$("#careMiles").val(careData.care_miles);
			$("#careMons").val(careData.care_mons);
			$("#cardDate").val(careData.card_date);
			$("#insureDate_m").val(careData.insure_date.split("-")[0]);
			$("#insureDate_d").val(careData.insure_date.split("-")[1]);
			
		});
	}
	
});